<template>
    <div>
        <div class="search">
            搜索盒子 &emsp;<el-input class="nameIpt" v-model="input_name" placeholder="请输入盲盒名称"></el-input>
            &emsp;&emsp;状态 <el-select @change="getwuxianshangList" v-model="value_zt" placeholder="--请选择状态--">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
            </el-select>
            <button class="sousuo"><i class="el-icon-search"></i></button>
        </div>
        <div class="btns">
            <button class="addBox" @click="addBox()">添加盒子</button>
            <button class="checkAdd">批量上架</button>
            <button class="checkDown">批量下架</button>
            <button class="checkDel">批量删除</button>
        </div>
        <!-- 赏品列表 -->
        <el-table border v-loading="loading" ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%;"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column label="ID" width="120px">
                <template v-slot="scope">{{ scope.row.id }}</template>
            </el-table-column>
            <el-table-column prop="name" label="盒子名称" width="120px">
            </el-table-column>
            <el-table-column prop="display_url" label="盒子图片" width="80" show-overflow-tooltip>
                <template v-slot="scope">
                    <div class="display_url_img_table">
                        <img :src="scope.row.display_url">
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="price" label="盒子金额" width="120" show-overflow-tooltip>
                <template v-slot="scope">
                    <el-button class="boxPrice" type="warning" size="small" round>￥{{
                        format_money(scope.row.price)
                    }}</el-button>
                </template>
            </el-table-column>
            <el-table-column prop="jiFen" label="是否返积分" width="75px" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="hidden" label="是否隐藏" width="75px" show-overflow-tooltip>
                <template v-slot="scope">
                    <el-button v-if="scope.row.hidden == 1" size="mini" type="success" round>是</el-button>
                    <el-button v-else size="mini" type="danger" round>否</el-button>
                </template>
            </el-table-column>
            <el-table-column prop="kuangNan" label="是否开启矿难" width="80px" show-overflow-tooltip>
                <template v-slot="scope">
                    <el-button v-if="scope.row.kuangNan == 1" size="mini" type="success" round>是</el-button>
                    <el-button v-else size="mini" type="danger" round>否</el-button>
                </template>
            </el-table-column>
            <el-table-column prop="baoDi" label="保底" width="80px" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="state" label="状态" width="80px" show-overflow-tooltip>
                <template v-slot="scope">
                    <el-button v-if="scope.row.state == 0" class="shangjia" size="small" type="danger"
                        round>下架</el-button>
                    <el-button v-else class="shangjia2" size="small" type="success" round>上架</el-button>
                </template>
            </el-table-column>
            <el-table-column prop="order" label="盒子排序" width="60px" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="address" label="操作" show-overflow-tooltip>
                <template v-slot="scope">
                    <div class="p1">
                        <a @click="dialogTableVisible = true">购买记录</a>
                        <a @click="edit(scope.row.uuid, scope.row.name)">编辑</a>
                        <el-button class="getGoods" @click="addwuxiangoods(scope.row.uuid)">添加赏品</el-button>
                    </div>
                    <div class="p2">
                        <a v-if="scope.row.state == 0" @click="changeState(scope.row.uuid, 1)">上架</a>
                        <a v-if="scope.row.state == 1" @click="changeState(scope.row.uuid, 0)">下架</a>
                        <a @click="del(scope.row.uuid)">删除</a>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pagenum"
            :page-sizes="[8, 10, 15]" :page-size="pagesize" layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
        <!-- 购买记录弹框 -->
        <el-dialog title="购买记录" :visible.sync="dialogTableVisible" width="60%">
            <div class="jilu_search">
                <el-input class="jilu_search_uid" v-model="jilu_uid" placeholder="会员UID"></el-input>
                <el-date-picker v-model="value1" type="datetimerange" start-placeholder="开始日期" end-placeholder="结束日期"
                    :default-time="['12:00:00']">
                </el-date-picker>
                <button class="sousuo"><i class="el-icon-search"></i></button>
                <hr color="#f7f7f7">
                <div class="jilu_span">
                    <span>(消费:)</span>
                    <span>(优惠券:)</span>
                    <span>(产出:0)</span>
                    <button class="sousuo geren_mingxi">个人明细查询</button>
                </div>
                <el-table class="jilu_table" :data="goumaijilu_tableData" border style="width: 100%">
                    <el-table-column prop="userInfo" label="用户信息" width="180">
                    </el-table-column>
                    <el-table-column prop="goodInfo" label="商品信息" width="180">
                    </el-table-column>
                    <el-table-column prop="gamename" label="赏名称">
                    </el-table-column>
                    <el-table-column prop="price" label="价格">
                    </el-table-column>
                    <el-table-column prop="buy_time" label="购买时间">
                    </el-table-column>
                    <el-table-column prop="number" label="编号">
                    </el-table-column>
                </el-table>
            </div>
        </el-dialog>

    </div>
</template>

<script>
import {StatusCodes} from 'http-status-codes'
import wuxian from "../../../api/jurisdiction"
import {format_money} from "@/utils/format";
export default {
    watch: {
        getNew(o, n) {
            o == 0 || n == 0
            this.getwuxianshangList()
        }
    },
    data() {
        return {
            total:0,
            loading: true,
            dialogTableVisible: false,
            input_name: '',
            value_zt: null,
            options: [
                  {
                    value: null,
                    label: '全部'
                }, {
                    value: 1,
                    label: '上架'
                },
                {
                    value: 0,
                    label: '下架'
                }],
            tableData: [],
            goumaijilu_tableData: [],
            wuxianshangInfo: {},
            multipleSelection: [],
            // 购买记录
            jilu_uid: '',
            // 选择时间
            value1: '',
            value2: '',
            pagesize: 8,
            pagenum: 1,
        }
    },
    created() {
        this.getwuxianshangList()
    },
    methods: {
      format_money,
        // 分页
        handleSizeChange(val) {
            this.page_size = val
            this.getwuxianshangList()
            console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {
            this.pagenum = val
            this.getwuxianshangList()
            console.log(`当前页: ${val}`);
        },
        // 获取无限池赏盒子列表信息
        getwuxianshangList() {
            wuxian.getwuxiangame({ params: { list: 1, page: this.pagenum, num: this.pagesize, state: this.value_zt, game_type: "wuxianshang" } }).then(res => {
                if (!res.status === StatusCodes.OK) {
                    this.$message.warning("请求无限赏列表失败")
                }
                console.log('所有盒子', res)
                if (res.status == 200) {
                    this.tableData = res.data
                    this.loading = false
                }
            }).catch(err => {
                    if (err.data.code === -10002) {
                        this.$message.warning("登录已过期")
                        this.$router.push("/login")
                    }
                    console.log(err)
                })
        },
        // 查询单个盒子信息
        getwuxianshangInfo(uuid) {
            wuxian.getwuxiangame({ uuid, name: "wuxianshang", }).then(res => {
            })
        },
        // 修改盒子
        edit(id) {
            this.$router.push({
                name: "tianjiawuxianshang",
                params: { id }
            })
        },
        // 添加
        addBox() {
            this.$router.push("/layout/choushangguanli/tianjiawuxianshang")
        },
        addwuxiangoods(id) {
            this.$router.push("/layout/choushangguanli/wuxianchigoods")
            this.$store.commit("SAVEWUXIANCHIUUID",id)
        },
        // 删除盒子
        del(uuid) {
            console.log(uuid)
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                wuxian.putwuxiangame({ uuid, deleted: true }).then(res => {
                    console.log(res)
                    this.getwuxianshangList()
                })
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        // 修改状态
        changeState(uuid, state) {
            wuxian.putwuxiangame({ uuid, state }).then((res) => {
                this.getwuxianshangList()
            })
        },
        toggleSelection(rows) {
            if (rows) {
                rows.forEach(row => {
                    this.$refs.multipleTable.toggleRowSelection(row);
                });
            } else {
                this.$refs.multipleTable.clearSelection();
            }
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
            console.log(val)
        },
        handleSizeChange(val) {
            this.page_size = val;
            this.getwuxianshangList()
            console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {
            this.pagenum = val;
            this.getwuxianshangList()
            console.log(`当前页: ${val}`);
        }
    }
}
</script>

<style lang="less" scoped>
.search {
    float: left;
    text-align: left;
    width: 100%;
    height: 40px;
    margin-bottom: 20px;
}

.el-pagination {
    margin-top: 20px;
    text-align: center;
}

.nameIpt {
    width: 200px;
}

.el-table {
    margin-top: 50px;
}

.sousuo {
    margin: 6px;
    width: 50px;
    height: 40px;
    background: #009688;
    border: 0;
    color: white;
    font-size: 20px;
    vertical-align: middle;
    text-align: center;
    line-height: 40px;
    border-radius: 4px;
    cursor: pointer;
}

.btns {
    text-align: left;
    width: 100%;
    height: 50px;
    margin: 20px auto;
}

.btns button {
    width: 150px;
    height: 35px;
    color: white;
    line-height: 30px;
    border: 0;
    margin: 0 10px;
    cursor: pointer;
}

.addBox {
    background: #009688;
}

.checkAdd,
.checkDown {
    background: #1e9fff;
}

.checkDel {
    background: #ff5722;
}

.boxPrice {
    color: white;
    padding: 7px 5px;
}

.p1 a,
.p2 a {
    background: #009688;
    color: white;
    font-size: 13px;
    padding: 2px 5px;
    margin: 5px;
    text-decoration: none;
    cursor: pointer;
}

.getGoods {
    height: 23px;
    border-radius: 23px;
    background: #009688;
    color: white;
    font-size: 13px;
    padding: 2px 5px;
    margin: 5px;
    text-decoration: none;
    cursor: pointer;
}

.p1 a {
    border-radius: 30px;
}

.p2 a {
    background: #ff5722;
}

.el-dialog {
    background: #f2f2f2;
}

.jilu_search_uid {
    width: 200px;
    margin-right: 20px;
}

.geren_mingxi {
    font-size: 14px;
    width: 130px;
    height: 40px;
    line-height: 40px;
    border-radius: 0;
}

.jilu_table {
    margin-top: 10px;
}

.jilu_span>span {
    display: inline-block;
    width: 70px;
    height: 25px;
    line-height: 25px;
    text-align: center;
    font-size: 13px;
    border: 1px solid #d1d1d1;
    margin-right: 10px;
    cursor: pointer;
}

.jilu_span>span:hover {
    border: 1px solid #009688;
}

.display_url_img_table {
    width:60px;
    height: 60px;
}

.display_url_img_table img {
    width: 60px;
    height: 60px;
}

.el-pagination {
    text-align: center !important;
    margin-top: 20px !important;
}

.chakanliebiao_table {
    width: 300px;
    height: 350px;
    overflow-y: scroll;
}

.chakanliebiao_table::-webkit-scrollbar {
    width: 0;
}
</style>